'use strict'

let watermark = {}

let setWatermark = strArr => {
    let id = '1.23452384164.123412415'

    if (document.getElementById(id) !== null) {
        document.body.removeChild(document.getElementById(id))
    }

    let can = document.createElement('canvas')
    can.width = 200
    can.height = 150

    let cans = can.getContext('2d')
    cans.rotate((-15 * Math.PI) / 150)
    cans.font = '14px 微软雅黑'
    // cans.fillStyle = 'rgba(196,178,178,0.3)'
    cans.fillStyle = '#DCDCDC'
    cans.textAlign = 'left'
    cans.textBaseline = 'Middle'
 //    cans.fillText(str, can.width / 6, can.height / 1)
	strArr.forEach((item, index) => {
		cans.fillText(item, can.width / 12, (can.height / 1) - 20 + index * 16)
	})

    let div = document.createElement('div')
    div.id = id
    div.style.pointerEvents = 'none'
    div.style.top = '15px'
    div.style.left = '0px'
    div.style.position = 'fixed'
    div.style.zIndex = '100000'
	div.style.opacity = '.5'
    div.style.width = document.documentElement.clientWidth + 'px'
    div.style.height = document.documentElement.clientHeight + 'px'
    div.style.background = 'url(' + can.toDataURL('image/png') + ') left top repeat'
    document.body.appendChild(div)
    return id
}

// 该方法只允许调用一次
watermark.set = strArr => {
    let id = setWatermark(strArr)
    setInterval(() => {
        if (document.getElementById(id) === null) {
            id = setWatermark(strArr)
        }
    }, 500)
    window.onresize = () => {
        setWatermark(strArr)
    }
}
// 移除水印
const outWatermark = id => {
    if (document.getElementById(id) !== null) {
        const div = document.getElementById(id)
        div.style.display = 'none'
    }
}
// 调用移除水印
watermark.out = () => {
    const str = '1.23452384164.123412415'
    outWatermark(str)
}

const watermarkSet = {
    createFlag: false,
    obj: {},
    textArr: '',
    //修改curid的值
    setCreateFlag(createFlag) {
        this.createFlag = createFlag
    },
    setObj(obj) {
        this.obj = obj
    }
}
export { watermark, watermarkSet }
